package com.liuhengjia.controller;

import com.liuhengjia.pojo.Result;
import com.liuhengjia.util.AliOSSs;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        // 获取文件内容的输入流，写入到本地磁盘文件
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null) {
            return Result.error("文件名为空");
        }
        String filename = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliOSSs.uploadFile(filename, file.getInputStream());
        return Result.success(url);
    }
}
